<template>
	<!-- 打卡统计信息主页 -->
	<!-- 此页面存在冗余 待优化 -->
	
	<view>
		<clock-overview :ft="filter_time" :data="overview_data" v-on:filter_change="handle_filter_change"></clock-overview>
		<view class="root-wrapper">
			<clock-card 
				v-for="(item,index) in l" 
				v-bind:info="item" >
			</clock-card>
		</view>
	</view>
	
</template>

<script>
	import clockCard from '../../components/clock-card.vue'
	import clockOverview from '../../components/clock-overview.vue'
	
	export default {
		components: {
			clockCard,
			clockOverview
		},
		onLoad(option) {
			// 初始加载数据
			let that = this
			let tmp_l = []
			
			// 上方的overview
			let tmp_overview ={
				'normal': 0,
				'abnormal': 0,
				'unfinished': 0
			}
			
			that.$u.api.getDeptTree().then(res => {
				let dept_id_map = that.$dept_tree_handler.get_dept_id_map(res.data[0])
				dept_id_map.forEach(ele => {
					
					if (ele.rel.indexOf(this.$store.state.deptId) != -1) {
						that.$u.api.getDeptSimpleInfo({
							'date': that.filter_time,
							'deptId': ele.id,
							'userId': 0
						}).then(res => {
							tmp_l.push({
								deptId: ele.id,
								name: ele.name,
								filter_time: that.filter_time,
								normal: res.data.clockedNumber - res.data.exceptionNumber,
								abnormal: res.data.exceptionNumber,
								unfinished:res.data.totalNumber - res.data.clockedNumber,
							})
							
							// 计算overview的和
							tmp_overview.normal += res.data.clockedNumber - res.data.exceptionNumber
							tmp_overview.abnormal += res.data.exceptionNumber
							tmp_overview.unfinished += res.data.totalNumber - res.data.clockedNumber
						
						})
					}
					
				})
				
				
				// 覆盖目标值
				that.l = tmp_l 
				that.overview_data = tmp_overview
				
			}).catch(err => {
				console.log('getDeptTree err', err)
			})
		},
		mounted() {
		
			console.log('查询日期', this.filter_time)
			console.log('查询部门', this.$store.state.deptId)
			
			// this.$u.api.getDeptSimpleInfo({
			// 	'date': this.filter_time,
			// 	'deptId': this.$store.state.deptId,
			// 	'userId': 0
			// 	// this.$store.state.deptId
			// }).then(res => {
			// 	this.overview_data = res.data
			// }).catch(err => {
			// 	console.log('err', err)
			// })
			
		},
		
		methods: {
			aggNormal: function() {
				let sum = 0 
				this.l.forEach(ele => {
					sum += ele.normal 
				})
				return sum
			},
			aggUnfinished: function() {
				let sum = 0 
				this.l.forEach(ele => {
					sum += ele.unfinished 
				})
				return sum
			},
			aggAbnormal: function() {
				let sum = 0 
				this.l.forEach(ele => {
					sum += ele.abnormal 
				})
				return sum
			},
			handle_filter_change: function(params) {
				console.log('响应了事件', params)
				this.filter_time = params
				
				let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
				let curRoute = routes[routes.length - 1].route
				let dir = curRoute.split('/')
				let cur_page = dir[dir.length - 1]
				
				
				let that = this
				let tmp_l = []
				let tmp_overview ={
					'normal': 0,
					'abnormal': 0,
					'unfinished': 0
				}
				that.$u.api.getDeptTree().then(res => {
					let dept_id_map = that.$dept_tree_handler.get_dept_id_map(res.data[0])
					dept_id_map.forEach(ele => {
						
						if (ele.rel.indexOf(this.$store.state.deptId) != -1) {
							that.$u.api.getDeptSimpleInfo({
								'date': that.filter_time,
								'deptId': ele.id,
								'userId': 0
							}).then(res => {
								tmp_l.push({
									deptId: ele.id,
									name: ele.name,
									filter_time: that.filter_time,
									normal: res.data.clockedNumber - res.data.exceptionNumber,
									abnormal: res.data.exceptionNumber,
									unfinished:res.data.totalNumber - res.data.clockedNumber,
								})
								
								tmp_overview.normal += res.data.clockedNumber - res.data.exceptionNumber
								tmp_overview.abnormal += res.data.exceptionNumber
								tmp_overview.unfinished += res.data.totalNumber - res.data.clockedNumber
							
							})
						}
						
					})
					
					// 覆盖目标值
					that.l = tmp_l 
					that.overview_data = tmp_overview
					
				}).catch(err => {
					console.log('getDeptTree err', err)
				})
				
			}
		},
		computed: {
			
		},
		data() {
			return {
				// filter_time默认为今天
				filter_time: this.$time_formatter.get_today(),	
				overview_data: {}, 	// 传入overview的对象
				l: [
					{
						name: '大数据1901',
						normal: 1000,
						abnormal:100,
						unfinished:10,
						
					},
					{
						name: '大数据1902',
						normal: 9000,
						abnormal:200,
						unfinished:20
					}
				]
			}
		}
	}
</script>

<style scoped>	
	.root-wrapper {
		 padding: 30rpx 0;
		display: flex;
		flex-direction: column;
		align-items: center;
	}
</style>
